/** Description of Settings.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.activities;

import java.text.NumberFormat;
import java.util.Comparator;
import java.util.Locale;
import java.util.SortedMap;
import java.util.TreeMap;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.widget.Toast;

import com.panther.awp.android.smoker.R;

public class Settings extends PreferenceActivity {
	
	String fNamePref;
	String lNamePref;
	String eMailIdPref;
	String currencyPref;
	Boolean statusPref;
	int avgSmokeMinutes;
	//Boolean isProtectionChecked;
	//String lockingPassword;
	//String phoneAppName;
	SortedMap<String, String> currencyMap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		
		// Get the custom preference
        CheckBoxPreference statusPrefrence = (CheckBoxPreference) findPreference("sStatus");
        statusPrefrence.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
			
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				if(!(Boolean)newValue) {
					Toast toast = Toast.makeText(Settings.this, "Stopped smoking in between ...", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20);
					toast.show();
					return true;
				} else {
					Toast toast = Toast.makeText(Settings.this, "You cannot smoke from settings page ...", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20);
					toast.show();
					return false;
				}
			}
		});
        
        //Currency List
        currencyMap = new TreeMap<String, String>(new Comparator<String>() {

			public int compare(String object1, String object2) {
				return object1.compareTo(object2);
			}
		});
        Locale[] allLocales = Locale.getAvailableLocales();
        for (Locale locale : allLocales) {
        	String name = locale.getDisplayCountry();
        	if(!name.equals("")) {
        		NumberFormat localFormat = NumberFormat.getCurrencyInstance(locale);
        		currencyMap.put(localFormat.getCurrency().getCurrencyCode(), name + "(" + localFormat.getCurrency().getSymbol(locale) + ")");
        	}
        }
        ListPreference currencyPref = (ListPreference) findPreference("yCurrency");
        CharSequence[] codeArr = currencyMap.keySet().toArray(new CharSequence[currencyMap.size()]);
        CharSequence[] nameArr = currencyMap.values().toArray(new CharSequence[currencyMap.size()]);
        currencyPref.setEntries(nameArr);
        currencyPref.setEntryValues(codeArr);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getPrefs();
	}

	private void getPrefs() {
        // Get the xml/preferences.xml preferences
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        currencyPref = prefs.getString("yCurrency", Locale.getDefault().getCountry());
        fNamePref = prefs.getString("fName", "Nothing has been entered");
        lNamePref = prefs.getString("lName", "Nothing has been entered");
        eMailIdPref = prefs.getString("eMailId", "Nothing has been entered");
        //isProtectionChecked = prefs.getBoolean("isPassEnabled", false);
        //lockingPassword = prefs.getString("lockPass", "No Locking Password Set");
        //phoneAppName = prefs.getString("yourAppName", "Smoker");
        statusPref = prefs.getBoolean("sStatus", false);
        try {
        	avgSmokeMinutes = Integer.parseInt(prefs.getString("avgTime", "5"));
        } catch (NumberFormatException e) {
			avgSmokeMinutes = 5;
		}
	}
	
}
